package com.Bigbluebook.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.Bigbluebook.Activity.PostDetailsActivity;
import com.Bigbluebook.R;
import com.Bigbluebook.entity.Post;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
    private List<Post> posts;
    private OnCommentSendListener commentSendListener;

    public interface OnCommentSendListener {
        void onCommentSend(String commentContent, int position);
    }

    public PostAdapter(List<Post> posts, OnCommentSendListener listener) {
        this.posts = posts;
        this.commentSendListener = listener;
    }

    @NonNull
    @Override
    public PostAdapter.PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_post_details,parent,false);
        return new PostViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PostAdapter.PostViewHolder holder, int position) {
        Post post = posts.get(position);
        holder.author.setText(post.getUser().getId());
        holder.content.setText(post.getContent());
        holder.time.setText(formatDate(post.getCreatedAt()));

//        // 设置按钮点击事件
//        holder.btnSendComment.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                // 获取评论内容
//                String commentContent = holder.btnComment.getText().toString();
//                Log.e("PostAdapter","评论内容" + String.valueOf(commentContent));
//                // 调用 Activity 中的 sendComment 方法
//                ((PostDetailsActivity) v.getContext()).sendComment(commentContent);
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return posts.size();
    }

    static class PostViewHolder extends RecyclerView.ViewHolder {
        TextView author;
        TextView content;
        TextView time;
        Button btnSendComment;
        EditText btnComment;
        public PostViewHolder(View itemView) {
            super(itemView);
            author = itemView.findViewById(R.id.author);
            content = itemView.findViewById(R.id.content);
            time = itemView.findViewById(R.id.create_time);
            btnSendComment = itemView.findViewById(R.id.btnSendComment);
            btnComment = itemView.findViewById(R.id.btnComment);
        }
    }

    public String formatDate(Date date) {
        if (date != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            return dateFormat.format(date);
        }
        return "";
    }
}
